const mongoose = require('mongoose');


mongoose.connect('mongodb://127.0.0.1:27017/users');

//建议用once连接数据库 once 只链接一次
mongoose.connection.once('open',() => {
    //创建文档（表）的结构
    let mySonSchema = mongoose.Schema({
        name:String,
        age:Number,
        //布尔类型
        flag:Boolean,
        //数组
        hobby:Array,
        //日期
        createData:Date,
        //任意类型
        mixedType:mongoose.Schema.Types.Mixed,

    });
    //创建模板对象
    let mySonModel = mongoose.model('mysons',mySonSchema);

    //插入文档
    mySonModel.create({
        name:"曹敬文",
        age:18,
        flag:true,
        hobby:['抽烟','喝酒','烫头'],
        createDate:new Date(),
        mixedType:'陈天宇是我爹'
    } ,(err,data) => {
        if (err) {
            console.log(err);
            return;
        }
        console.log(data);
        //断开连接
        mongoose.disconnect();
    });
});
mongoose.connection.on('close',() => {
    console.log('连接关闭');
});
mongoose.connection.on('error',() => {
    console.log('连接失败');
});